/*年月日时分秒转化*/
const formatTime = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return [month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
}

const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
}

/*年月日转化*/
const formatTime1 = date => {
    date.setTime(date.getTime() - 24 * 60 * 60 * 1000);
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()

    return [year, month, day].map(formatNumber).join('-')
}


/*截止日期时间转化*/
function formatEndTime(a) {
    var hh = parseInt(a / 3600);
    var length = ''
    if (hh < 10) hh = "0" + hh;
    var mm = parseInt((a - hh * 3600) / 60);
    if (mm < 10) mm = "0" + mm;
    var ss = parseInt((a - hh * 3600) % 60);
    if (ss < 10) ss = "0" + ss;

    if (hh > 0) {
        length = hh + "小时后过期";
    } else {
        if (mm > 0) {
            length = mm + "分钟后过期";
        } else {
            if (ss > 0) {
                length = ss + "秒钟后过期";
            } else {
                length = '已过期'
            }
        }
    }

    if (a > 0) {
        return length;
    } else {
        return "NaN";
    }
}

module.exports = {
    formatTime,
    formatEndTime,
    formatTime1,
    host:'https://gzh.zhenheyiguan.com/'
}
